package com.bjut.vehicleshow.service;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.bjut.vehicleshow.dao.CarTrainingMapper;
import com.bjut.vehicleshow.pojo.CarTrainingDO;
import com.bjut.vehicleshow.util.GPSUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Comparator;
import java.util.List;
import java.util.stream.Collectors;

/**
 * All rights Reserved, Designed By www.sac.com
 *
 * @author 王文贵
 * @version V1.0.0
 * @projectName sac-web-rpc-server
 * @title StuServiceImpl
 * @package com.sac.scada.rpc.web.stu.service
 * @description
 * @date 2020/2/12
 * @copyright www.sac.com
 * 注意 本内容仅限于国电南自,禁止外泄以及用于其他的商业
 */
@Service
public class CarTrainingServiceImpl implements CarTrainingService {

    @Autowired
    private CarTrainingMapper carTrainingMapper;

    @Override
    public List<String> getAllCarName() {
        List<String> collect = carTrainingMapper
                .selectList(null)
                .stream().map(x -> x.getCarName())
                .distinct()
                .collect(Collectors.toList());
        collect.sort(Comparator.comparingInt(x -> Integer.parseInt(x.split("_")[1])));
        return collect;
    }

    @Override
    public List<CarTrainingDO> getByName(String carName) {
        final List<CarTrainingDO> carList = carTrainingMapper
                .selectList(new QueryWrapper<CarTrainingDO>().eq("CarName", carName));
        for (CarTrainingDO carTrainingDO : carList) {
            double[] locations = GPSUtil.gps84_To_Gcj02(carTrainingDO.getLatitude(), carTrainingDO.getLongtitude());
            carTrainingDO.setLatitude(locations[0]);
            carTrainingDO.setLongtitude(locations[1]);
        }
        return carList;
    }
}
